package controller;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class DataManager
{
        private ArrayList<Profile> profiles = new ArrayList<Profile>();
        private Profile currentProfile;// reach preferences of current user by calling currentProfile.getPreferences()
        private Preferences preferences;
        private int currentIndex;
        private int profileCount;
        private ArrayList<Integer> highScoresList ;
        private ArrayList<String> usernameListOfhighScores; 
        
        public DataManager()
        {		
                currentProfile = null;
                currentIndex = 0;
                profileCount = 0;
                highScoresList = new ArrayList<Integer>();
                for ( int i = 0; i<5; i++)
                	highScoresList.add(0);
                usernameListOfhighScores= new ArrayList<String>();
                for ( int i = 0; i<5; i++)
                	usernameListOfhighScores.add("");
                  }
        
        public DataManager(String file) throws Exception
        {
                FileInputStream fileIn = new FileInputStream(file);
                ObjectInputStream inStream = new ObjectInputStream(fileIn);
                
                profiles = (ArrayList<Profile>)inStream.readObject();
                
                currentProfile = null;
                currentIndex = 0;
                profileCount = profiles.size();
                
                inStream.close();
        }
        public void save(String file) throws Exception
        {
                FileOutputStream fileOut = new FileOutputStream(file);
                ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
                
                outStream.writeObject(profiles);
                
                outStream.flush();
                outStream.close();
        }
        public void load(String file) throws Exception
        {
                FileInputStream fileIn = new FileInputStream(file);
                ObjectInputStream inStream = new ObjectInputStream(fileIn);
                
                profiles = (ArrayList<Profile>)inStream.readObject();
                
                currentProfile = null;
                currentIndex = 0;
                profileCount = profiles.size();
                
                inStream.close();
        }
        
        public boolean addProfile(String username, String password)
        {
        	//this method will return true if adding new profile is successful
        	boolean alreadyExist = false;
    		for (int i = 0; i < profiles.size(); i++)
    			if(profiles.get(i).getUsername().equals( username)){
    				alreadyExist = true;
    				break;
    			}
    		if(alreadyExist == false)	{
    			Preferences pref = new Preferences();
    			boolean[] b = new boolean[3];
    			System.out.println(b[1]);
    			Profile p = new Profile(username, password, 0, pref, b); 
    			profiles.add(p);
    			profileCount++;
    			currentProfile = p;
    			return true;
    		}
    		else//the username already exists, can not be registered again
    			return false;		
            
        }
        
        public void updatePreferences(String username, Preferences pref)
        {
                int index = -1;
                
                for(Profile p : profiles)
                {
                        if(p.getUsername().equals(username))
                                index = profiles.indexOf(p);
                }
                System.out.println("current username: "+ currentProfile.getUsername());
                if(index != -1)
                        profiles.get(index).setPreferences(pref);       
        }
        public void transmit(String backgroundChoice,String barChoice,String puckChoice){
        	preferences = new Preferences();
        	//setBackground
        	if(backgroundChoice.equals("1")){
        		//preferences.setBackground();
        	}
        	else if(backgroundChoice.equals("1")){
        		//preferences.setBackground();
        	}
        	else{
        		//preferences.setBackground();
        	}
        	System.out.println("current username: "+ currentProfile.getUsername());
        	//setBarColor
        	if(puckChoice.equals("Blue Bar")){
        		//preferences.setBarImage();
        	}
        	else if(puckChoice.equals("Black Bar")){
        		//preferences.setBarImage();
        	}
        	else{//It means Red Bar
        		//preferences.setBarImage();
        	}
        	
        	//setpuckColor
        	if(backgroundChoice.equals("Red Puck")){
        		//preferences.setPuckImage();
        	}
        	else if(backgroundChoice.equals("Blue Puck")){
        		//preferences.setPuckImage();
        	}
        	else{
        		//preferences.setPuckImage();
        	}
        	//currentProfile.setPreferences(preferences);
        	
        }
        public Profile retrieveInfo(String username)
        {
                int index = -1;
                
                for(Profile p : profiles)
                {
                	if(p.getUsername().equals(username)){
                		index = profiles.indexOf(p);
                		break;
                	}
                		
                }
                
                if(index == -1)
                        return null;
                else
                        return profiles.get(index);
        }
        
        public boolean authenticate(String username, String password)
        {        	
                int index = -1;
                
                for(Profile p : profiles)
                {
                        if(p.getUsername().equals(username))
                        {
                                index = profiles.indexOf(p);
                                break;
                        }              
                }
                if(index != -1 && profiles.get(index).getPassword().equals(password)){
                	currentProfile = profiles.get(index);
                	currentIndex = index;
                	//totalScoreOfCurrentProfile = profiles.get(index).getTotalScore();
                	return true;
                }
                        
                else
                        return false;
        }
        
        public void setCurrentIndex(int i)
        {
                currentIndex = i;
        }
        
        public int getCurrentIndex()
        {
                return currentIndex;
        }
        public String getCurrentUsername(){
        	return currentProfile.getUsername();
        }
        public Profile getCurrentProfile(){
        	return currentProfile;
        }
        public int getScoreOfCurrentUser(){
        	return currentProfile.getTotalScore();
        }
        public ArrayList<Integer>getHighScoresList(){
        	 return highScoresList;
        }
        public ArrayList<String>getUsernameListOfHighScores(){
        	return usernameListOfhighScores;
        }
        public void sortHighScoresList(Profile p){
        		System.out.println("profile name is: " + p.getUsername());
        		System.out.println("score of that user is: " + p.getTotalScore());
        		int counter = 0;
                for(int i = 0; i<5 ; i++){
                	if(p.getTotalScore() >= highScoresList.get(i)&& counter == 0){
                		counter++;
               		 	highScoresList.add(i,p.getTotalScore());               		 		
               		 	usernameListOfhighScores.add(i,p.getUsername());
               		 	for(int ix = 0; ix<5; ix++){
            		 		System.out.println(ix + ". " +"username: " + usernameListOfhighScores.get(ix));
            		 		System.out.println("score: " + highScoresList.get(ix));
            		 	}
               		 	break;
               		}
               	 }  
        }

        
}